home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / OSAX Samples / Use WildCards OSAX / GetFontInfo3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-07  |  3.4 KB  |  140 lines  |  [TEXT/KAHL]

  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    GetFontInfo2.c
  4. //    Written by Donald Olson
  5. //    Copyright ®1993 Apple Computer Inc.
  6. //    All rights reserved.
  7. //
  8. //    This is a simple Scripting Addition that returns the Font (info) of the
  9. //    specified font and size. If the size parameter is not specified, default
  10. //    size of 12 is used. This demonstrates the use of a wildCard event ID
  11. //    as a dispatch mechanism inside your code. Written for a WWDC 
  12. //    presentation by Donald Olson and Donn Denman.
  13. //
  14. //    Syntax: font (leading | ascent | descent | max width) <font name> [size <short>]
  15. //    Returns: record {leading:short;ascent:short;descent:short; max width: short}
  16. //
  17. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18.  
  19. // Our header
  20. Boolean GetFontNumber( Str255 fontName, short *fontNum );
  21.  
  22. // Our Includes
  23. #include <string.h>
  24. #include <Quickdraw.h>
  25. #include <Fonts.h>
  26. #include <Errors.h>
  27. #include <AppleEvents.h>
  28.  
  29. /*struct FontInfo {
  30.     short ascent;
  31.     short descent;
  32.     short widMax;
  33.     short leading;
  34. };
  35. */
  36. //AEVTFINF****
  37.  
  38. #define kLeading    'lead'
  39. #define kAscent        'sent'
  40. #define kDescent    'desc'
  41. #define kMaxWidth    'mxwd'
  42.  
  43. #define keyFONTSIZE    'FSIZ'
  44.  
  45. pascal OSErr main(AppleEvent *theEvent, 
  46.                 AppleEvent *theReply, 
  47.                 long theRefCon) 
  48. {        
  49.     OSErr        theErr = noErr;
  50.     GrafPort        fontPort;
  51.     GrafPtr        savedPort, fontPortPtr = &fontPort;
  52.     Str255        fontName;
  53.     long            actualSize;
  54.     short        fontSize, fontNum;
  55.     DescType    typeCode;
  56.     FontInfo        fInfo;
  57.     DescType    ourID;
  58.     
  59.     theErr = AEGetAttributePtr(theEvent, keyEventIDAttr, 
  60.                 typeKeyword, &typeCode, (Ptr)&ourID, 
  61.                 sizeof(ourID), &actualSize);
  62.                 
  63.     theErr = AEGetParamPtr(theEvent, keyDirectObject, 
  64.                 typeChar, &typeCode, (Ptr)&fontName, 
  65.                 sizeof(fontName), &actualSize);
  66.     
  67.     if(theErr != noErr)
  68.         return theErr;
  69.     
  70.     fontName[actualSize] = '\0';    // c string please
  71.     c2pstr((char*)fontName);    // and now pascal
  72.     
  73.     theErr = AEGetParamPtr(theEvent, keyFONTSIZE, 
  74.                 typeShortInteger, &typeCode, (Ptr)&fontSize, 
  75.                 sizeof(fontSize), &actualSize);
  76.     
  77.     if(theErr != noErr)
  78.         fontSize = 12;
  79.     
  80.     if(!GetFontNumber(fontName, &fontNum))
  81.         return fontDecError;
  82.                 
  83.     GetPort(&savedPort);
  84.     OpenPort(&fontPortPtr);
  85.     SetPort(&fontPortPtr);
  86.     
  87.     TextFont(fontNum);
  88.     TextSize(fontSize);
  89.     
  90.     GetFontInfo(&fInfo);
  91.     
  92.     SetPort(&savedPort);
  93.     
  94.     switch(ourID) {
  95.         case kLeading:
  96.             theErr = AEPutParamPtr(    theReply, keyDirectObject, 
  97.                                     typeShortInteger, 
  98.                                     (Ptr)&fInfo.leading, 
  99.                                     sizeof(fInfo.leading));
  100.             break;
  101.         case kAscent:
  102.             theErr = AEPutParamPtr(    theReply, keyDirectObject,
  103.                                     typeShortInteger, 
  104.                                     (Ptr)&fInfo.ascent, 
  105.                                     sizeof(fInfo.ascent));
  106.             break;
  107.         case kDescent:
  108.             theErr = AEPutParamPtr(    theReply, keyDirectObject, 
  109.                                     typeShortInteger, 
  110.                                     (Ptr)&fInfo.descent, 
  111.                                     sizeof(fInfo.descent));
  112.             break;
  113.         case kMaxWidth:
  114.             theErr = AEPutParamPtr(    theReply, keyDirectObject, 
  115.                                     typeShortInteger, 
  116.                                     (Ptr)&fInfo.widMax, 
  117.                                     sizeof(fInfo.widMax));
  118.             break;
  119.         default:
  120.             break;
  121.     }
  122.     
  123.     return theErr;
  124. }
  125.  
  126. Boolean GetFontNumber( Str255 fontName, short *fontNum )
  127. {
  128.     Str255     systemFontName;
  129.  
  130.     GetFNum( fontName, fontNum );
  131.     if ( *fontNum == 0) {
  132.         /* Either we didn't find the font, or we were looking for the system
  133.           * font. */
  134.         GetFontName( 0, systemFontName );
  135.         return EqualString( fontName, systemFontName, FALSE, FALSE );
  136.     }
  137.     else
  138.         return TRUE;
  139. }
  140.